<?php
/**
 * @title 1038. 从二叉搜索树到更大和树
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param TreeNode $root
     * @return TreeNode
     */
    function bstToGst($root) {
        /**
         * @since 2020-09-09 转成数组
         */
        $list = self::TreeToArray($root);
        // return implode(",", $list);

        /**
         * @since 2020-09-09 叠加
         */
        $hashs = [];
        $sum = 0;
        for ($i=0; $i<count($list); $i++){
            $sum += $list[$i];
            $hashs[$list[$i]] = $sum;
            $list[$i] = $sum;
        }
        // return [implode(",", $list), $hashs];

        /**
         * @since 2020-09-09 修改二叉树
         */
        $next = [$root];
        while($next){
            $nextTmp = [];

            /**
             * @since 2020-09-09 遍历每个点
             */
            foreach ($next as $node){
                $node->val = $hashs[$node->val];

                /**
                 * @since 2020-09-09 左右子树
                 */
                if($node->left !== null){
                    $nextTmp[] = $node->left;
                } else {}
                if($node->right !== null){
                    $nextTmp[] = $node->right;
                } else {}
            }

            /**
             * @since 2020-09-09 下次迭代
             */
            $next = $nextTmp;
        }

        /**
         * @return
         */
        return $root;
    }

    /**
     * @title 二叉树变array, right-root-left
     * @author start2004
     * @since 2020-09-09 Created
     *
     * @param $root
     * @return array
     */
    public function TreeToArray($root){
        /**
         * @since 2020-09-09 结果集
         */
        $result = [];

        /**
         * @since 2020-09-09 右子树
         */
        if($root->right !== null){
            $result = self::TreeToArray($root->right);
        } else {
            // $result = ["null"];
        }

        /**
         * @since 2020-09-09 节点值
         */
        $result[] = $root->val;

        /**
         * @since 2020-09-09 左子树
         */
        if($root->left !== null){
            $result = array_merge($result, self::TreeToArray($root->left));
        } else {
            // $result[] = "null";
        }

        /**
         * @return
         */
        return $result;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202009/2020.09.08_2.php
 */
$datas = [
    [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->bstToGst($xhprof->tree($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". $data[1][$i][0] ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
